/*
 * TextPrompt.java
 *
 *  created: 14.9.2012
 *  charset: UTF-8
 */

package cz.mp.k3bg.gui.component;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;


/**
 * <i>Prompt</i> pro textové komponenty.
 * <p>
 * Odvozeno z
 * <a href="http://tips4java.wordpress.com/2009/11/29/text-prompt/"
 * http://tips4java.wordpress.com/2009/11/29/text-prompt/</a>.
 * <p>
 * <pre>
 * {@literal TextPrompt promptPreferredDateFormat = new TextPrompt("YYYY-MM-DD");}
 * {@literal promptPreferredDateFormat.setToComponent(dateField);}
 * </pre>
 * 
 * @author Martin Pokorný
 * @version 0.1
 */
public class TextPrompt extends JLabel implements DocumentListener {

	private Document document;
    private String prompt;

    
    public TextPrompt() {
    }
        
    /**
     * 
     * @param prompt
     */
    public TextPrompt(String prompt) {        
        this.prompt = prompt;        
    }

    /**
     * 
     * @param component 
     */
    public void setToComponent(JTextComponent component) {
		document = component.getDocument();
		document.addDocumentListener(this);

		setPromptImpl(prompt);
		setFont(component.getFont());
		setForeground(component.getForeground());
		setBorder(new EmptyBorder(component.getInsets()));
		setHorizontalAlignment(JLabel.LEADING);
        setAlpha(0.35f);
                
		component.setLayout(new BorderLayout());
		component.add(this);
		updatePromptVisibility();
    }
    
    /**
     * 
     * @return 
     */
    public String getPrompt() {
        return prompt;
    }

    /**
     * 
     * @param prompt 
     */
    public void setPrompt(String prompt) {
        setPromptImpl(prompt);
    }
    
    private void setPromptImpl(String prompt) {
        this.prompt = prompt;
        setText(prompt);
    }

	/**
	 *
	 * @param alpha  0.0f - 1.0f
	 */
	public void setAlpha(float alpha)
	{
		setAlpha((int)(alpha * 255));
	}

	/**
     * 
     * @param alpha  0-255
     */
	public void setAlpha(int alpha) {
        int alphaIn = alpha;
        if (alpha > 255) {
            alphaIn = 255;
        }
        else if (alpha < 0) {
            alphaIn = 0;
        }		

		Color foreground = getForeground();
		int red = foreground.getRed();
		int green = foreground.getGreen();
		int blue = foreground.getBlue();

		Color withAlpha = new Color(red, green, blue, alpha);
		super.setForeground( withAlpha );
	}
    
    private void updatePromptVisibility() {
        setVisible(true);
        if (document.getLength() > 0) {
			setVisible(false);
		}
    }
    
    // -- DocumentListener
    public void insertUpdate(DocumentEvent e) {
        if (document == null) {
            return;
        }
        updatePromptVisibility();
    }

    public void removeUpdate(DocumentEvent e) {
        if (document == null) {
            return;
        }
        updatePromptVisibility();
    }

    public void changedUpdate(DocumentEvent e) {
    }

}   // TextPrompt.java
